<?php 
namespace app\admin\controller;
use think\Controller;

class Conf extends Controller{

	public function index(){
		if(request()->isPost()){
			$data = input('post.');
			// dump($data);die;
			foreach($data['sort'] as $k=>$v){
				db('conf')->where("id = $k")->update(['sort' => $v]);
			}
		}
		$res = db('conf')->order('sort asc')->paginate(15);
		$this->assign([
			'res'	=>$res,
		]);
		return view();
	}

	public function add(){
		if(request()->isPost()){
			$data = input('post.');
			$data['values'] = str_replace('，',',',$data['values']);
			$res = db('conf')->insert($data);
			if($res){
				$this->success('添加配置成功！',url('index'));
			}else{
				$this->error('添加配置失败！');
			}
		}
		return view();
	}

	public function edit($id){
		if(request()->isPost()){
			$data = input('post.');
			$res = db('conf')->update($data);
			if($res){
				$this->success('修改配置成功！',url('index'));
			}else{
				$this->error('修改配置失败！');
			}
		}
		$res = db('conf')->find($id);
		$this->assign([
			'res'	=>$res,
		]);
		return view();
	}

	public function del($id){
		$res = db('conf')->delete($id);
		if($res){
			$this->success('配置删除成功！',url('index'));
		}else{
			$this->error('配置删除失败！');
		}
	}

	public function conflist(){
		$conf = db('conf');
		if(request()->isPost()){
			$data = input('post.');
			$checkField2D=$conf->field('ename')->where(array('form_type'=>'checkbox'))->select();
			$checkFields=array();
			if($checkField2D){
				foreach($checkField2D as $k=>$v){
					$checkFields[] = $v['ename'];
				}
			}
			$allFields = array();
			foreach($data as $k => $v){
				$allFields[] = $k;
				if(is_array($v)){
					$value = implode(',',$v);
					$conf->where(array('ename' => $k))->update(['value'=>$value]);
				}else{
					$conf->where(array('ename' => $k))->update(['value'=>$v]);
				}
			}
			foreach($checkFields as $k=>$v){
				if(!in_array($v,$allFields)){
					$conf->where(array('ename'=>$v))->update(['value'=>'']);
				}
			}
			if($_FILES){
				foreach($_FILES as $k => $v){
					if($v['tmp_name']){
						$imgs=$conf->field('value')->where(array('ename'=>$k))->find();
						if($imgs['value']){
							$oimg=IMG_UPLOAD.$imgs['value'];
							if(file_exists($oimg)){
								@unlink($oimg);
							}
						}
						$imgSrc=$this->upload($k);
						$conf->where(['ename'=>$k])->update(['value'=>$imgSrc]);
					}
				}

			}
			$this->success('配置修改成功！',url('conflist'));
		}
		$shopConfRes = $conf->where(['conf_type'=>1])->select();
		$goodsConfRes = $conf->where(['conf_type'=>2])->select();
		$this->assign([
			'shopConfRes'	=>$shopConfRes,
			'goodsConfRes'	=>$goodsConfRes,
		]);
		return view();
	}

	//上传图片
	public function upload($imgName){
	    // 获取表单上传文件 例如上传了001.jpg
	    $file = request()->file($imgName);
	    // 移动到框架应用根目录/public/uploads/ 目录下
	    if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'static'. DS .'upload');
	        if($info){
	            return $info->getSaveName();
	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	            die;
	        }
	    }
	}

}